package com.pxy.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity implements TextWatcher {
    //PDA扫码广播
    private ScanReceiver mScanReceiver;
    private IntentFilter intentFilter;
    public static final String WOW_NEW_ADD = "com.android.receive_scan_action";
    public static final String WOW_NEW_KEY = "data";
  private TextView mTextView;
    private ArrayList<Pharmacy> mStrings=new ArrayList<>();
    private ArrayList<B> mBs=new ArrayList<>();

    private ArrayList<DoorPatientInfoBeen> mDoorPatientInfoBeens=new ArrayList<>();
    private EditText mEditText;
    private EditText mEditText2;
    private AutoCompleteTextView mAutoCompleteTextView1;
    private AutoCompleteTextView mAutoCompleteTextView2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView= (TextView) findViewById(R.id.tv);

        mAutoCompleteTextView1= (AutoCompleteTextView) findViewById(R.id.num_fact_doorScan_result1);
        mAutoCompleteTextView2= (AutoCompleteTextView) findViewById(R.id.num_fact_doorScan_result2);

//        mEditText.addTextChangedListener(this);
//        mEditText2.addTextChangedListener(this);
        mScanReceiver = new ScanReceiver();
        intentFilter = new IntentFilter();
         intentFilter.addAction(WOW_NEW_ADD);
        registerReceiver(mScanReceiver, intentFilter);
        mStrings.add(new Pharmacy("P0103-1"));
        mStrings.add(new Pharmacy("P1103-1"));
        mStrings.add(new Pharmacy("P0106-1"));
        mStrings.add(new Pharmacy("P0104-1"));
        mStrings.add(new Pharmacy("P1109-1"));
        mStrings.add(new Pharmacy("P1203-1"));
        mStrings.add(new Pharmacy("P0203-1"));
        mStrings.add(new Pharmacy("P1903-1"));
        mStrings.add(new Pharmacy("P2003-1"));
        mStrings.add(new Pharmacy("Z2003-1"));
        mStrings.add(new Pharmacy("ZJ2003-1"));


//        mBs.add(new B("P0103-1"));
//        mBs.add(new B("P1103-1"));
//        mBs.add(new B("P0106-1"));
//        mBs.add(new B("P0104-1"));
//        mBs.add(new B("P1109-1"));
//        mBs.add(new B("P1203-1"));
//        mBs.add(new B("P0203-1"));
//        mBs.add(new B("P1903-1"));
//        mBs.add(new B("P2003-1"));
//        Log.e("TAG", "onCreate前: "+ mStrings.toString());
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("A011-4"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("A022-5"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("Z033-4"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("Z011-4"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("Z022-4"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("Z013-4"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("A222-4"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("A333-4"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("AZ99-4"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("ZA44-4"));


        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("Z0101-1"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("Z0204-4"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("ZJ01-04"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("ZJ04-04"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("ZM04-04"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("ZM04-04"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("P2105-2"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("P5105-2"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("P1905-2"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("P1105-2"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("P0404-4"));
//        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("PJ09-04"));
        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("PJ11-04"));
        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("PJ11-04"));
        mDoorPatientInfoBeens.add(new DoorPatientInfoBeen("PJ11-04"));

//        Pharmacy[] objects =  mStrings.toArray(new Pharmacy[0]);
//        Arrays.sort(objects,new MyComparator());
//        List<Pharmacy> pharmacies = Arrays.asList(objects);
//        Log.e("PXY", "onCreate前: "+ mDoorPatientInfoBeens.toString());
//        Collections.sort(mDoorPatientInfoBeens,new MyCompareNew<DoorPatientInfoBeen>());
//        Collections.sort(mBs,new MyComparator<B>());
//        Log.e("PXY", "onCreate后: "+ mDoorPatientInfoBeens.toString());
//        String str1="po12345";
//        Log.e("PXY", "length: "+str1.length() );
//        Log.e("PXY", "substring: "+str1.substring(0,str1.length()) );
//        Log.e("PXY", "substring: "+str1.substring(0,6) );
//        Log.e("PXY", "substring: "+str1.substring(0,7) );
        mBs.add(new B("2017-09-11 16:09:20"));
        mBs.add(new B("2017-08-21 06:09:15"));
        mBs.add(new B("2015-07-21 06:09:15"));
        mBs.add(new B("2017-06-21 06:09:15"));
        mBs.add(new B("2016-01-21 06:09:15"));
        mBs.add(new B("2017-09-21 06:09:15"));
        mBs.add(new B("2017-09-21 06:09:15"));
//       Log.e("PXY", "onCreate前: "+ mBs.toString());
//        Collections.sort(mBs, new DataCompare());
////        Collections.sort(mBs,new MyComparator<B>());
//        Log.e("PXY", "onCreate后: "+ mBs.toString());




        String data1="2017-09-11 16:09:20";
        String data2="2017-08-21 06:09:15";
        String data3="2017-07-21 06:09:15";
        String data4="2017-06-21 06:09:15";
        String data5="2016-01-21 06:09:15";
        String data6="2017-09-21 06:09:15";
        String data7="2017-09-21 06:09:15";
//        SimpleDateFormat sdf =   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
//        try {
//            Date parse1 = sdf.parse(data1.toString());
//            Date parse2 = sdf.parse(data2.toString());
//            Log.e("PXY", "parse1: "+ parse1.toString());
//            Log.e("PXY", "parse2: "+ parse2.toString());
//            Log.e("PXY", "compareTo: "+ parse1.compareTo(parse2));
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }


//          String str1="2222222";
//          String str2="-1";
//        String str3="j";
//        String str4="9j";
//        Log.e("'PXY", "str1: "+ isNumeric(str1));
//        Log.e("'PXY", "str2: "+ isNumeric(str2));
//        Log.e("'PXY", "str3: "+ isNumeric(str3));
//        Log.e("'PXY", "str4: "+ isNumeric(str4));
//        String str1="C1987195|3406156";
//        String substring = str1.substring(0, 1);
//        if(str1.substring(0, 1).equalsIgnoreCase("C")){
//            int i = str1.indexOf("|");
//            String zuHao=str1.substring(1,str1.indexOf("|"));
//            String ID=str1.substring(str1.indexOf("|")+1,str1.length());
//            Log.e("'PXY", "substring: "+substring);
//            Log.e("'PXY", "i: "+i);
//            Log.e("'PXY", "zuHao: "+zuHao);
//            Log.e("'PXY", "ID: "+ Integer.parseInt("123" + "9999"));
//            int i1 = Integer.parseInt("123" + "9999");
//        }

    }
    public  boolean test(){
        if (null != mDoorPatientInfoBeens && mDoorPatientInfoBeens.size() > 0) {
            for (DoorPatientInfoBeen i : mDoorPatientInfoBeens) {
                if (null != i.getKFWZMC() && i.getKFWZMC().equals("PJ11-04")
                        ) {
                    //匹配成功
                    if (i.isCheck()) {
//                        Log.e("XXX1", "onCreate: " +mDoorPatientInfoBeens.toString());
//                        return false;
                        continue;
                    }
                    i.setCheck(true);
                    Log.e("XXX3", "onCreate: " +mDoorPatientInfoBeens.toString());
                    return true;
                }
            }
            Log.e("XXX2", "onCreate: " +mDoorPatientInfoBeens.toString());
        }
        return false;
    }

    public void go1(View view) {
    Log.e("PXY1", "go1:  "+isWifi(this) );
    }

    public void go2(View view) {
        test();
    }

    public void go3(View view) {

    }
    /**
     * 利用正则表达式判断字符串是否是数字
     * @param str
     * @return
     */
    public boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    //TODO:接收PDA扫码广播，准备跳转
    class ScanReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(WOW_NEW_ADD)) {
                Log.e("TAG", "onReceive: "+intent.getStringExtra(WOW_NEW_KEY) );
                mTextView.setText("当前识别的二维码是: "+intent.getStringExtra(WOW_NEW_KEY));
            }
        }
    }

    /**
     * make true current connect service is wifi
     * @param mContext
     * @return
     */
    private  boolean isWifi(Context mContext) {
        ConnectivityManager connectivityManager = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null
                && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return true;
        }
        return false;
    }
}
